angular.module('Controller')

.controller('QgyConfigureTypeCtrl', ($scope,NgTableParams,menuManage,wfAlert,$localStorage,commonUtil,commonConstant,$rootScope,$uibModal,$http) ->
    if $rootScope.pageType=='configure-type'
        $scope.info=$rootScope.pageParams
        $rootScope.pageType=undefined
    initSearchData = ()->
        $scope.search = {
            apartmentId:$scope.info.apartmentId,
            name:undefined
        }
    initSearchData()

    $scope.enabledList=commonConstant.enabledList(true)
    $scope.payWayList=commonConstant.payWayList(true)
    $scope.repairStatusList=commonConstant.repairStatusList(true)
    $scope.needFeeList=[
        {value:true,name:'是'},
        {value:false,name:'否'}
    ]
    loadApartment=()->
        $scope.apartmentList=[{id:undefined,name:'全部'}]
        $http.get('/apartment',params:{page:0,size:999})
        .success (resp)->
            if resp.code==0
                $scope.apartmentList=$scope.apartmentList.concat(resp.message.content)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    loadApartment()
    $scope.tableParams = new NgTableParams(
            {page: 1, count: commonConstant.pageTableCount},
            {
                counts: [5, 10, 20],
                getData: ($defer, params)->
                    $scope.doQuery($defer,params)
                    return ;
            }
    )

    $scope.doQuery=($defer, params)->
        reqData={
            page: params.page() - 1,
            size: params.count(),
            name:$scope.search.name
        }
        reqData.startTime=moment($scope.search.startTime).format('YYYY-MM-DD') if $scope.search.startTime
        reqData.endTime=moment($scope.search.endTime).format('YYYY-MM-DD') if $scope.search.endTime
        $http.get('/configureType',params:reqData)
        .success (resp)->
            if resp.code==0
                $scope.tableData=resp.message.content
                params.total(resp.message.totalElements)
                $defer.resolve($scope.tableData)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    _.extend $scope,
        newData: ->
            data={
                apartmentId:$scope.info.apartmentId
                apartmentName:$scope.info.apartmentName
            }
            showModal('new',data)
        editData:(item)->
            theData=angular.copy(item)
            theData.apartmentName=$scope.info.apartmentName
            showModal('edit',theData)
        configData:(item)->
            theData=angular.copy(item)
            showConfigModal('edit',theData)
        resetSearch: ->
            initSearchData()
        queryData: (keepPage)->
            if(keepPage != true)
                $scope.tableParams.page(1)
            $scope.tableParams.reload()
        removeData:(item)->
            $http.delete('/configureType/'+item.id)
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo('房间标配删除成功');
                    $scope.queryData();
                else
                    wfAlert.toastError(resp.message)
            )
    showModal=(type,data)->
        $scope.modal = $uibModal.open({
            animation: true,
            appendTo: $("#modalContent:eq(0)"),
            controller: 'QgyConfigureTypeModalCtrl',
            templateUrl: 'templates/project-manage/apartment-configure-type/configure-type-modal.html',
            resolve: {
                items:  ()->{type: type, data: data}
            }
        })
        $scope.modal.result.then((modalData)->
            $scope.queryData()
        )
    showConfigModal=(type,data)->
        $scope.modal = $uibModal.open({
            animation: true,
            appendTo: $("#modalContent:eq(0)"),
            controller: 'QgyEditConfigureModalCtrl',
            templateUrl: 'templates/project-manage/apartment-configure-type/edit-configure-modal.html',
            resolve: {
                items:  ()->{type: type, data: data}
            }
        })
        $scope.modal.result.then((modalData)->
            $scope.queryData()
        )
)